# docker load image
if docker images | grep ondh/nginx-fancyindex > /dev/null 2>&1;then
  echo "Nginx-fancyindex image existed"
else
  docker load -i nginx-fancyindex.tar
  if docker images | grep ondh/nginx-fancyindex > /dev/null 2>&1;then echo "Load image success";fi
fi

# clean image whouys-fancyindex:latest
if docker images | grep whouys-fancyindex > /dev/null 2>&1;then 
  docker rmi -f whouys-fancyindex:latest
  echo "Clean whouys-fancyindex image success"
fi

# build image
echo
echo "Start build docker image..."
docker build -f Dockerfile -t whouys-fancyindex:latest .

if docker images | grep whouys-fancyindex > /dev/null 2>&1;then echo "Docker build image success";fi

# deploy
echo
echo "Deploy whouys-fancyindex:"
echo "Run: docker run -d -it --restart always --name file-download -v your-file-path:/data -p your-port:80 whouys-fancyindex:latest"

# start testing
echo
echo "Run test: docker run -it --rm -v ./:/data -p 8989:80 whouys-fancyindex:latest"
read -p "Start test whouys-fancyindex image (y/n): " input
if [[ "$input" =~ ^[Yy]$ ]]; then
  echo
  echo "starting..."
  ipaddr=$(ip -4 addr show scope global | grep -v 'docker\|veth\|br-' | grep 'inet' | awk '{print $2}'|cut -d'/' -f1|head -n1)
  echo "http://$ipaddr:8989"
  docker run -it --rm -p 8989:80 -v ./:/data whouys-fancyindex:latest
else
  echo "exit"
  exit 1
fi
